package de.vocabulary.client.editor;

import java.util.List;

import org.gwt.advanced.client.datamodel.ComboBoxDataModel;
import org.gwt.advanced.client.ui.widget.ComboBox;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.vocabulary.client.VocabularyServiceProvider;
import de.vocabulary.client.service.VocabularyService;
import de.vocabulary.client.service.VocabularyServiceAsync;
import de.vocabulary.shared.model.Vocable;

public class VocableEditor extends Composite  implements Editor<Vocable>{
	TextBox language1Word;
	ComboBox<ComboBoxDataModel> language2Word;
	Button button_delete;
	Button button_getTranslation;
	//StupidOracle oracle;
	private ComboBoxDataModel model;
	private Label errorMsg;

	private VocabularyServiceAsync service;
	private VocableTable parent;
	private VocableEditor editor;

	

	public VocableEditor() {


		HorizontalPanel h = new HorizontalPanel();

		h.setSpacing(1);
		language1Word = new TextBox();
		language1Word.addKeyUpHandler(new KeyUpHandler() {
				@Override
			public void onKeyUp(KeyUpEvent event) {
				int	keyPress = event.getNativeKeyCode();
				if (keyPress == KeyCodes.KEY_ENTER)
					parent.gotoNextField(true, editor);
				else if (keyPress == KeyCodes.KEY_UP)
					parent.goUp(true, editor);
				else if (keyPress == KeyCodes.KEY_DOWN)
					parent.goDown(true, editor);
				else if (keyPress == KeyCodes.KEY_RIGHT)
					parent.goRight(true, editor);
			}

		});
		
		VerticalPanel v = new VerticalPanel();
		 language2Word = new ComboBox<ComboBoxDataModel>();
		 model = new ComboBoxDataModel();
		 language2Word.setModel(model);
		 language2Word.setCustomTextAllowed(true);
		 language2Word.setLazyRenderingEnabled(false);
		
		/*oracle = new StupidOracle();  

		language2Word = new SuggestBox(oracle);
		language2Word.getValueBox().addFocusHandler(new FocusHandler() {

			@Override
			public void onFocus(FocusEvent event) {
				language2Word.showSuggestionList();

			}
		});*/
		
		language2Word.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				int	keyPress = event.getNativeKeyCode();
				if (keyPress == KeyCodes.KEY_ENTER)
					parent.gotoNextField(false, editor);
				else if (keyPress == KeyCodes.KEY_UP)
					parent.goUp(false, editor);
				else if (keyPress == KeyCodes.KEY_DOWN)
					parent.goDown(false, editor);
				else if (keyPress == KeyCodes.KEY_LEFT)
					parent.goLeft(false, editor);
				
			}

		});
		
		errorMsg = new Label();
		errorMsg.setStylePrimaryName("smallError");
		errorMsg.setText("No translation found");
		errorMsg.setVisible(false);
		
		button_delete = new Button();
		button_delete.setTitle("Delete vocable");
		button_delete.setText("Delete");


		button_delete.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				parent.removeVocable(editor);

			}

		});


		button_getTranslation = new Button();
		button_getTranslation.setTitle("Get translation");
		button_getTranslation.setText("Get Translation");
		button_getTranslation.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				service.getTranslation(language1Word.getValue(), new GetTranslationCallback());

			}

		});
		
		 

		h.add(language1Word);
		h.add(v);
			v.add(language2Word);
			v.add(errorMsg);
		h.add(button_getTranslation);
		h.add(button_delete);
		//		h.add(form);
		initWidget(h);

	}


	public void onLoad(){
		//initialize with initial vocabulary
		service =  VocabularyServiceProvider.getService();
		parent = (VocableTable) this.getParent();
		editor = this;
	}

	


	public void setTabIndex(int row, boolean verticalEditing){
		/*//to make it start with 1 not 0
		row++;
		//set tab indexing
		if (verticalEditing){
			language1Word.setTabIndex(row);
			language2Word.setTabIndex(-1);
		}
		else{
			language1Word.setTabIndex(2*row);
			language2Word.setTabIndex(2*row+1);
		}*/

	}



	public void setSuggestions(List<String> list) {
		/*if (language2Word.getText().equals("") && list!=null && list.size()>0)
			language2Word.setText(list.get(0)); //list.get(0);*/
		//if (list.size()<1) return;
		model.clear();
		if (list.size()>0){
			int i=0;
			for (String translation:list)
				 model.add("id_"+i, translation); //" אין"
			errorMsg.setVisible(false);
			language2Word.select(0);
			language2Word.setText(list.get(0));
			
		}
		else{
			errorMsg.setVisible(true);
		}
		
	/*	if (language2Word.getValue().equals("") && list!=null && list.size()>0)
			language2Word.setValue(list.get(0));
		oracle.setSuggestions(list);*/
	}


	public class GetTranslationCallback implements AsyncCallback<List<String>> {

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());

		}


		@Override
		public void onSuccess(List<String> result) {
			setSuggestions(result);

		}

	}

}
